﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Collections;

namespace WebVideo.Areas.Admin.Models
{
    public class SupportAdminModel
    {
        public int Id { get; set; }
        public String UserName { get; set; }
        [Required(ErrorMessage = "Nội dung trả lời không được để trống")]
        [StringLength(4000, ErrorMessage = "Nội dung phải trả lời ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 5)]
        public string Contents { get; set; }
        public DateTime Date { get; set; }
        public string IdRoot { get; set; }
        public static IEnumerable<EntitySupportAdmin> LayDuLieu()
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntitySupportAdmins
                        where p.IdRoot == null
                        select p;
            return query.ToList();
        }
        public static void Insert(SupportAdminModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            //edit 
            EntitySupportAdmin old = db.EntitySupportAdmins.Where(g=>g.Id == Mtnew.Id).First();
            old.IdRoot = "1";
            db.SubmitChanges();
            //insert
            EntitySupportAdmin tnew = new EntitySupportAdmin();
            string ad = db.EntityAdmins.First().UserName;
            tnew.UserName = ad; 
            tnew.Contents = Mtnew.Contents;
            tnew.IdRoot = Mtnew.UserName;
            tnew.Date = old.Date;
            db.EntitySupportAdmins.InsertOnSubmit(tnew);
            db.SubmitChanges();
        }
        public static void Delete(int Id)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntitySupportAdmin SupportAdmin = db.EntitySupportAdmins.Where(p => p.Id == Id).Single();
            db.EntitySupportAdmins.DeleteOnSubmit(SupportAdmin);
            db.SubmitChanges();
        }
 
    }
}